package com.wenshi.app.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.wenshi.app.activity.R;
import com.wenshi.view.IKeyValue;
import com.wenshi.view.IWsView;
import com.wenshi.view.WsVIewClickListener;
import com.wenshi.view.WsViewTools;





public class WsLoadMoreListView extends ListView implements AbsListView.OnScrollListener, IWsView {
    private WsSwipeRefreshAndLoadMoreLayout.IRefreshAndLoadMoreLisenter refreshAndLoadMoreLisenter;
    private String[] clicks;
    private View rooterView;
    private boolean isHaveMoreData = true;// 是否有更多数据(默认为有)
    private ProgressBar progressBar;
    private TextView tipContext;

    private WsSwipeRefreshAndLoadMoreLayout mRefreshAndLoadMoreView;
    private boolean isLoading = false;// 是否正在加载
    private OnListViewScrollListener onListViewScrollListener=null;

    public interface OnListViewScrollListener {
        void onScrollStateChanged(AbsListView view, int scrollState);


        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }

    public WsLoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //动态载入底部布局
        Log.e("views","WsLoadMoreListView");
        rooterView = LayoutInflater.from(context).inflate(WsViewTools.getResource(getContext(),"wsview_pull_to_load_footer","layout"), null);
//        progressBar = (ProgressBar) rooterView.findViewById(R.id.footer_progressbar);
        progressBar = (ProgressBar) rooterView.findViewById(  WsViewTools.getResource(getContext(),"footer_progressbar","id"));

//        tipContext = (TextView) rooterView.findViewById(R.id.footer_hint_textview);
        tipContext = (TextView) rooterView.findViewById(WsViewTools.getResource(getContext(),"footer_hint_textview","id"));
        //向listView的底部添加布局(此时当给listView设置Item点击事件的时候，默认不触发这个添加的布局的点击事件)
        addFooterView(rooterView, null, false);
        setOnScrollListener(this);
        if (context instanceof WsSwipeRefreshAndLoadMoreLayout.IRefreshAndLoadMoreLisenter) {
            Log.e("views","refreshAndLoadMoreLisenter");
            refreshAndLoadMoreLisenter = (WsSwipeRefreshAndLoadMoreLayout.IRefreshAndLoadMoreLisenter) context;
        }
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WsElement);
       // TypedArray typedArray = getContext().obtainStyledAttributes(attrs, WsViewTools.getWsAttrsIds(context));
        clicks = WsViewTools.initAttrs(this, context, typedArray);


    }
    public void setWsSwipeRefreshAndLoadMoreLayout(WsSwipeRefreshAndLoadMoreLayout v) {

            mRefreshAndLoadMoreView = v;
    }


    public void setHaveMoreData(boolean isHaveMoreData,boolean isinit) {
        this.isHaveMoreData = isHaveMoreData;
        if (!isHaveMoreData) {
            if(isinit)
            {

            }else{
                tipContext.setText("只有这么多啦");
                progressBar.setVisibility(View.GONE);
            }

        } else {
            if(isinit)
            {
                tipContext.setVisibility(View.GONE);
                tipContext.setText("正在加载");
                progressBar.setVisibility(View.GONE);
            }else{
                tipContext.setVisibility(View.VISIBLE);
                tipContext.setText("正在加载");
                progressBar.setVisibility(View.VISIBLE);
            }

        }
    }

    public void onLoadComplete() {
        isLoading = false;
    }

    public boolean isLoading() {
        return isLoading;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        Log.e("views", "onScrollStateChanged: "+view.getLastVisiblePosition()+"#"+(view.getCount() - 1) +"#"+mRefreshAndLoadMoreView.isRefreshing());
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            if (view.getLastVisiblePosition() == view.getCount() - 1 && (mRefreshAndLoadMoreView != null &&
                    !mRefreshAndLoadMoreView.isRefreshing()) && !isLoading && refreshAndLoadMoreLisenter != null && isHaveMoreData) {
                isLoading = true;

              //  Log.e("views", "onScrollStateChanged_in: "+isLoading);
                refreshAndLoadMoreLisenter.onLoadMore();
            }
        }
        if( this.onListViewScrollListener!=null)
        {
            this.onListViewScrollListener.onScrollStateChanged(view,scrollState);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if( this.onListViewScrollListener!=null)
        {
            this.onListViewScrollListener.onScroll(view,firstVisibleItem,visibleItemCount,totalItemCount);
        }
    }

    @Override
    public void bindData(IKeyValue data) {
//        if (mRefreshAndLoadMoreView == null && this.getParent() instanceof WsSwipeRefreshAndLoadMoreLayout) {
//            Log.e("views","WsSwipeRefreshAndLoadMoreLayout");
//            mRefreshAndLoadMoreView = (WsSwipeRefreshAndLoadMoreLayout) this.getParent();
//        }
    }


    @Override
    public String[] getClick() {
        return new String[0];
    }


    @Override
    public void bindData(IKeyValue data, WsVIewClickListener listener) {
        bindData(data);
        clicks = WsViewTools.initAttrsByData((Activity) this.getContext(), data, clicks);
    }

    @Override
    public String getClassName() {
        return null;
    }


}
